package org.sivan.game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by 湛兮 on 2017/2/8.
 */
abstract public class HGame extends Frame {
    private boolean resizeable = false;

    private JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            render(g);
        }
    };

    public HGame() {
        add(panel, BorderLayout.CENTER);
        init();
        setLocationRelativeTo(null);
        update();
        setResizable(resizeable);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                exit();
            }
        });
    }

    protected void init() {
    }

    protected void render(Graphics g) {
    }

    protected void onMouseClick(MouseEvent event) {
    }

    protected void onKeyPress(KeyEvent event) {
    }

    protected void onKeyRelease(KeyEvent event) {
    }

    protected void update() {
    }

    protected void exit() {
        System.exit(0);
    }

    public void setSize(int width, int height) {
        super.setSize(width, height);
    }

    public void setTitle(String title) {
        super.setTitle(title);
    }

    public void setResizeable(boolean resizeable) {
        this.resizeable = resizeable;
    }

    public void setWindow(String title, int width, int height) {
        super.setTitle(title);
        super.setSize(width, height);
    }

    public void echo(String text) {
        System.out.print(text);
    }

    public void echoln(String text) {
        System.out.println(text);
    }


}